<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 短语拼写检查
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Text_Spell
{

    /**
     * 拼写参考短语集
     *
     * @var array
     */
    protected $_referList = array();

    /**
     * 设置参考短语
     *
     * @param  string|array  $phrase
     * @param  string        $refer
     * @return EGP_Spell
     */
    public function setRefer($phrase, $refer)
    {
        if (!is_array($phrase)) {
            $phrase = array($phrase);
        }

        if (isset($this->_referList[$refer])) {
            $this->_referList[$refer] = array_merge($this->_referList[$refer], $phrase);
        } else {
            $this->_referList[$refer] = $phrase;
        }

        return $this;
    }

    /**
     * 获取所有参考短语
     *
     * @return array
     */
    public function getAllPhrases()
    {
        return array_keys($this->_referList);
    }

    /**
     * 获取所有参考
     *
     * @return array
     */
    public function getAllReferMap()
    {
        return $this->_referList;
    }

    /**
     * 获取文本拼写建议
     *
     * @param  string  $text
     * @param  boolean  $ignoreCase
     * @return array
     */
    public function getSuggest($text, $ignoreCase = true)
    {
        ($ignoreCase === true) && $text = strtolower($text);

        $suggest = array();
        foreach ($this->_referList as $refer => $phrase) {
            foreach ($phrase as $p) {
                if ($ignoreCase === true) {
                    $p = strtolower($p);
                }
                $count = substr_count($text, $p);
                if ($count > 0) {
                    $suggest[] = array(
                        'phrase' => $p,
                        'refer'  => $refer,
                        'times'  => $count,
                    );
                }
            }
        }
        return $suggest;
    }

    /**
     * 根据建议替换文件
     *
     * @param  string   $text
     * @param  boolean  $ignoreCase
     * @return string
     */
    public function suggestReplace($text, $ignoreCase = true)
    {
        ($ignoreCase === true) && $text = strtolower($text);
        foreach ($this->_referList as $refer => $phrase) {
            foreach ($phrase as $p) {
                $func = ($ignoreCase === true) ? 'str_ireplace' : 'str_replace';
                $text = $func($p, $refer, $text);
            }
        }
        return $text;
    }

}